<template>
	<view class="home">
		<swiper indicator-dots autoplay circular>
			<swiper-item v-for="(item, index) in swipers" :key="index">
				<image :src="item.src" mode=""></image>
			</swiper-item>
		</swiper>
		<!-- 导航 -->
		<view class="nav">
			<view class="nav-item" v-for="(item, index) in navs" :key="index" @click="goToDetail(item.path)">
				<view :class="item.icon"></view>
				<text>{{item.title}}</text>
			</view>
		</view>
		<!-- 推荐商品 -->
		<view class="hot-goods">
			<view class="title">推荐商品</view>
			<goods-list @clickGoodsItem="goGoodsDetail" :goodsList="goodsList"></goods-list>
		</view>
	</view>
</template>

<script>
	import { reqSwipers, reqGoodsList } from '@/api'
	import goodsList from '@/components/goods-list/goods-list'
	export default {
		data() {
			return {
				swipers: [],
				goodsList: [],
				navs: [
					{
						icon: 'iconfont icon-Newuserzone',
						title: 'uni超市',
						path: '/pages/goods/goods'
					},
					{
						icon: 'iconfont icon-certified-supplier',
						title: '联系我们',
						path: '/pages/contactUs/contactUs'
					},
					{
						icon: 'iconfont icon-pic',
						title: '社区图片',
						path: '/pages/comPics/comPics'
					},
					{
						icon: 'iconfont icon-video',
						title: '学习视频',
						path: '/pages/learnVideo/learnVideo'
					}
				],
				pageNum: 1, // 商品页码
				total: 0, // 总页码
			}
		},
		onLoad() {
			this.getSwipers()
			this.getGoodsList(1)
		},
		components: {
			'goods-list': goodsList
		},
		methods: {
			// 获取轮播图
			async getSwipers() {
				const result = await reqSwipers()
				if(result.code !== 200) {
					return uni.showToast({
						title:'数据获取失败！'
					})
				}
				this.total = result.total
				this.swipers = result.data
			},
			// 获取商品列表
			async getGoodsList(pageNum) {
				const result = await reqGoodsList(pageNum)
				if(result.code !== 200) {
					return uni.showToast({
						title:'数据获取失败！'
					})
				}
				this.goodsList = result.data
			},
			// 跳转到导航路由页面
			goToDetail(url) {
				uni.navigateTo({
					url
				})
			},
			// 跳转到详情页
			goGoodsDetail(id) {
				uni.navigateTo({
					url: `/pages/goods/goods-detail?id=${id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.home {
		swiper {
			width: 750rpx;
			height: 380rpx;
			image {
				width: 100%;
				height: 100%;
			}
		}
	}
	.nav {
		display: flex;
		.nav-item {
			width: 25%;
			text-align: center;
			view {
				width: 120rpx;
				height: 120rpx;
				line-height: 120rpx;
				background-color: $shop-color;
				border-radius: 50%;
				margin: 20rpx auto;
				font-size: 60rpx;
				color: #fff;
			}
			text {
				font-size: 30rpx;
			}
		}
	}
	.hot-goods {
		background-color: #eee;
		overflow: hidden;
		margin-top: 20rpx;
		.title {
			height: 100rpx;
			line-height: 100rpx;
			color: $shop-color;
			text-align: center;
			letter-spacing: 40rpx;
			background-color: #fff;
			margin: 8rpx 0;
		}
	}
</style>
